package com.firefly.service.query.cms;

import com.firefly.dao.dao.cms.mapper.CategoryMapper;
import com.firefly.dao.assembler.cms.CategoryAssembler;
import com.firefly.dao.dto.CommonDto;
import com.firefly.dao.vo.cms.CategorySelectVo;
import com.firefly.dao.vo.cms.CategoryTreeVo;
import com.firefly.dao.vo.cms.CategoryVo;
import com.firefly.util.utils.TreeNodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @description: 栏目
 * @author: lzg(萤火科技 ： 854378082 @ qq.com)
 * @date: 2022/12/20
 **/
@Service
public class CategoryQuery {
    private final CategoryAssembler categoryAssembler;
    private final CategoryMapper categoryMapper;
    @Autowired
    public CategoryQuery(CategoryAssembler categoryAssembler,CategoryMapper categoryMapper){
        this.categoryAssembler = categoryAssembler;
        this.categoryMapper = categoryMapper;
    }
    // 获取所有
    public List<Map> tree() {
        // 查出所有栏目来
        List<CategoryVo> list = categoryMapper.getAll();
        // 类型转换
        List<CategoryTreeVo> listTree = categoryAssembler.getTreeList(list);
        //树形结构数据生成
        List<Map> result = TreeNodeUtils.toTree(0, listTree.stream().collect(Collectors.toList()));
        return result;
    }

    public List<Map> select() {
        // 查出所有栏目来
        List<CategoryVo> list = categoryMapper.getAll();
        // 类型转换
        List<CategorySelectVo> listSelect = categoryAssembler.getSelectListVo(list);
        //树形结构数据生成
        List<Map> result = TreeNodeUtils.toSelect(0, list.stream().collect(Collectors.toList()));
        return result;
    }

    // 查单条带父id
    public CategoryVo getDetail(CommonDto request) {
        return categoryMapper.getDetail(request.detailId);
    }

}
